package it.controllocredito.wizard.ui;

import it.controllocredito.R;
import it.controllocredito.wizard.model.WeekDayInfoPage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Spinner;

public class WeekDayInfoFragment extends Fragment{
    private static final String ARG_KEY = "key";
    
    private PageFragmentCallbacks mCallbacks;
    private String mKey;
    private WeekDayInfoPage mPage;
    private Spinner mWeekDayView;

    LayoutInflater inflater = null;
    
    public static WeekDayInfoFragment create(String key) {
        Bundle args = new Bundle();
        args.putString(ARG_KEY, key);

        WeekDayInfoFragment fragment = new WeekDayInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public WeekDayInfoFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        mKey = args.getString(ARG_KEY);
        mPage = (WeekDayInfoPage) mCallbacks.onGetPage(mKey);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	this.inflater = inflater;
    	View rootView = inflater.inflate(R.layout.wizard_fragment_page_weekday_info, container, false);
        ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

        mWeekDayView = (Spinner) rootView.findViewById(R.id.weekdays_spinner);
               
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(rootView.getContext(),
	             R.array.weekdays_array, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    mWeekDayView.setAdapter(adapter);
        
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof PageFragmentCallbacks)) {
            throw new ClassCastException("Activity must implement PageFragmentCallbacks");
        }

        mCallbacks = (PageFragmentCallbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mWeekDayView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				mPage.getData().putString(WeekDayInfoPage.WEEKDAY_DATA_KEY,
                        (parent != null) ? parent.getSelectedItem().toString() : null);
                mPage.notifyDataChanged();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent)
			{
				Log.v("routes", "nothing selected");
			}
        });

    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);

        // In a future update to the support library, this should override setUserVisibleHint
        // instead of setMenuVisibility.
        if (mWeekDayView != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            if (!menuVisible) {
                imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
            }
        }
    }

}